<template>
  <view class="page">
    <view class="header">
      <image class="lock-icon" src="/static/images/settings/lock.png" mode="aspectFit"></image>
      <view class="header-text">
        <view class="title">修改登录密码</view>
        <view class="subtitle">为保障账户安全，请定期更换密码</view>
      </view>
    </view>
    
    <view class="form-card">
      <wd-cell-group custom-class="custom-group">
        <!-- 当前密码 -->
        <wd-input
          v-model="form.oldPassword"
          label="当前密码"
          clearable
          show-password
          placeholder="请输入当前密码"
          input-align="right"
          custom-class="password-input"
        />
        
        <!-- 新密码 -->
        <wd-input
          v-model="form.newPassword"
          label="新密码"
          clearable
          show-password
          placeholder="请输入新密码"
          input-align="right"
          custom-class="password-input"
        />
        
        <!-- 确认新密码 -->
        <wd-input
          v-model="form.confirmPassword"
          label="确认新密码"
          clearable
          show-password
          placeholder="请再次输入新密码"
          input-align="right"
          custom-class="password-input"
        />
      </wd-cell-group>
      
      <view class="tip-card">
        <view class="tip-title">
          <wd-icon name="warning" size="28rpx" color="#FF9C19" />
          <text>密码设置须知</text>
        </view>
        <view class="tip-content">
          <view class="tip-item">
            <wd-icon name="check" size="24rpx" color="#07C160" />
            <text>长度为8-20个字符</text>
          </view>
          <view class="tip-item">
            <wd-icon name="check" size="24rpx" color="#07C160" />
            <text>必须包含字母和数字</text>
          </view>
          <view class="tip-item">
            <wd-icon name="check" size="24rpx" color="#07C160" />
            <text>不能与旧密码相同</text>
          </view>
        </view>
      </view>
      
      <view class="button-wrapper">
        <wd-button type="info" block custom-class="submit-btn" @tap="submitForm">确认修改</wd-button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { updatePassword } from '@/api/user'

const form = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 验证密码格式
const validatePassword = (password) => {
  // 密码长度为8-20位，必须包含字母和数字
  const reg = /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,20}$/
  return reg.test(password)
}

// 提交表单
const submitForm = async () => {
  // 验证表单
  if (!form.value.oldPassword) {
    uni.showToast({
      title: '请输入当前密码',
      icon: 'none'
    })
    return
  }
  
  if (!form.value.newPassword) {
    uni.showToast({
      title: '请输入新密码',
      icon: 'none'
    })
    return
  }
  
  if (!validatePassword(form.value.newPassword)) {
    uni.showToast({
      title: '新密码格式不正确，需包含字母和数字，长度8-20位',
      icon: 'none'
    })
    return
  }
  
  if (form.value.newPassword !== form.value.confirmPassword) {
    uni.showToast({
      title: '两次输入的密码不一致',
      icon: 'none'
    })
    return
  }
  
  if (form.value.oldPassword === form.value.newPassword) {
    uni.showToast({
      title: '新密码不能与旧密码相同',
      icon: 'none'
    })
    return
  }
  
  try {
    // 显示加载状态
    uni.showLoading({
      title: '提交中...'
    })
    
    // 调用修改密码接口
    await updatePassword({
      oldPwd: form.value.oldPassword,
      newPwd: form.value.newPassword,
      newConfirmPwd: form.value.confirmPassword
    })
    
    // 隐藏加载状态
    uni.hideLoading()
    
    uni.showToast({
      title: '密码修改成功',
      icon: 'success'
    })
    
    // 清空表单
    form.value = {
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    }
    
    // 返回上一页
    setTimeout(() => {
      uni.navigateBack()
    }, 1500)
  } catch (error) {
    // 隐藏加载状态
    uni.hideLoading()
    
    console.error('密码修改失败:', error)
    uni.showToast({
      title: error.message || '密码修改失败',
      icon: 'none'
    })
  }
}
</script>

<style lang="scss">
.page {
  min-height: 100vh;
  background: #f8f8f8;
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}

.header {
  display: flex;
  align-items: center;
  padding: 40rpx 30rpx;
  
  .lock-icon {
    width: 120rpx;
    height: 120rpx;
    margin-right: 24rpx;
  }
  
  .header-text {
    flex: 1;
    
    .title {
      font-size: 36rpx;
      font-weight: bold;
      color: #333;
      margin-bottom: 10rpx;
    }
    
    .subtitle {
      font-size: 26rpx;
      color: #999;
    }
  }
}

.form-card {
  margin: 0 24rpx;
  background: #fff;
  border-radius: 12rpx;
  overflow: hidden;
  box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.05);
  padding: 30rpx 0;
  
  :deep(.custom-group) {
    padding: 0;
    
    &::after,
    &::before {
      display: none;
    }
    
    .password-input {
      padding: 28rpx 24rpx;
      
      .wd-input__label {
        color: #666;
        font-size: 28rpx;
      }
      
      .wd-input__input {
        color: #333;
      }
    }
  }
}

.tip-card {
  margin: 30rpx 24rpx;
  background: rgba(255, 156, 25, 0.08);
  border-radius: 8rpx;
  overflow: hidden;
  
  .tip-title {
    display: flex;
    align-items: center;
    padding: 16rpx 20rpx;
    background: rgba(255, 156, 25, 0.15);
    
    text {
      font-size: 26rpx;
      color: #FF9C19;
      font-weight: 500;
      margin-left: 10rpx;
    }
  }
  
  .tip-content {
    padding: 16rpx 20rpx;
    
    .tip-item {
      display: flex;
      align-items: center;
      margin: 10rpx 0;
      
      text {
        font-size: 24rpx;
        color: #666;
        margin-left: 10rpx;
      }
    }
  }
}

.button-wrapper {
  margin: 40rpx 24rpx 20rpx;
  
  .submit-btn {
    height: 88rpx;
    border-radius: 44rpx;
    font-size: 32rpx;
    border: none;
  }
}
</style> 